﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gpsys.Entity.MSG;
using Gpsys.Web.Common.Filter;
using Gpsys.Web.Business;
using Gpsys.Web.Models;

namespace Gpsys.Web.Controllers
{
    public class QueueController : BaseController
    {
        //
        // GET: /Queue/
        [CompressFilter]
        [HttpPost]
        public ActionResult Pagination(QueueTablePaginationRequest model)
        {
            var qb = new QueueBusiness();

            

            List<Queue> qs = qb.GetQueuesForPagination(model);

            var qsNew = qs.Select(q => new
            {
                id = string.Format("<a href=\"{1}\" target=\"_blank\">{0}</a>", q.Id, Url.Action("Detail", "Queue", new {@Id = q.Id})),
                type = q.Type,
                flow = q.Flow,
                tRN = q.TRN,
                currency = !string.IsNullOrEmpty(q.Currency) ? q.Currency : "",
                amount = q.Amount.HasValue ? q.Amount.ToString() : "",
                lastLog = q.LastLog,
                modifiedOn = q.ModifiedOn.HasValue ? q.ModifiedOn.ToString() : ""
            });

            return this.Json(new{ sEcho = model.sEcho,
                                  iTotalRecords = qs.Count.ToString(),
                                  iTotalDisplayRecords =  model.TotalRow.ToString(),
                                  aaData = qsNew.ToArray() }); 
        }

        private void SetSelectedMenu()
        {
            ViewBag.SelectedMenu = "Queue";
        }

        public ActionResult Index(QueueTablePaginationRequest model)
        {
            SetSelectedMenu();

            return View();
        }

        public ActionResult Detail(long Id)
        {
            SetSelectedMenu();
            QueueModel model = new QueueModel();

            model.LoadQueue(Id);

            if (model.Queue == null)
            {
                return RedirectToAction("e404", "Error");
            }
            else
            {
                return View(model);
            }
        }

    }
}
